﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ServiceModel;

namespace ExameRegister
{
    public class ServiceUtility 
    {
        public event EventHandler<WCFEvent> OnCompletedCallService;

        //public static string uri = "http://sysjlxbbm.xh.sh.cn/Sevice/Service1.svc";//31.0.65.233
        //public static string uri = "http://localhost:2560/Sevice/Service1.svc";//31.0.65.233
        //public static string uri = "http://31.0.65.233/Sevice/Service1.svc";
        //public static string uriFiles = "http://localhost:5335/";
        //public static string uri = "http://192.168.5.100:6666/Sevice/Service1.svc";  ///http://192.168.5.100:6666/Sevice/Service1.svc
        public static string uri = "http://192.168.15.147:2222/Sevice/Service1.svc";
        //public static string uri = "http://192.168.1.101:6666/Sevice/Service1.svc";
        //public static string uri = "http://192.168.141.1:6666/Sevice/Service1.svc";//31.0.65.233
        private IService1 s = null;
        private DependencyObject _uc;
        //private object _UserState = null;
        public void CallService(string ClassName, string MethodName, object[] Param, DependencyObject uc)
        {
            ObservableCollection<string> _Param = new ObservableCollection<string>();
            _uc = uc;
            if (Param != null)
            {
                foreach (var p in Param)
                {
                    _Param.Add(Utility.ConvertToJson(p));
                }
            }
            EndpointAddress endpointAddress = new EndpointAddress(uri);
            s = new ChannelFactory<IService1>(new BasicHttpBinding() { MaxReceivedMessageSize = 2147483647, MaxBufferSize = 2147483647 }, endpointAddress).CreateChannel();
            s.BeginAction(ClassName, MethodName, _Param, CallBack, null);
        }

        public void CallService(string ClassName, string MethodName, object[] Param, DependencyObject uc, object UserState)
        {
            //_UserState = UserState;
            ObservableCollection<string> _Param = new ObservableCollection<string>();
            _uc = uc;
            if (Param != null)
            {
                foreach (var p in Param)
                {
                    _Param.Add(Utility.ConvertToJson(p));
                }
            }
            EndpointAddress endpointAddress = new EndpointAddress(uri);
            s = new ChannelFactory<IService1>(new BasicHttpBinding(), endpointAddress).CreateChannel();
            s.BeginAction(ClassName, MethodName, _Param, CallBack, UserState);
        }

        private void CallBack(IAsyncResult ar)
        {
            
            string obj = s.EndAction(ar);
            object userstate = ar.AsyncState;
            //if (obj.GetType().Namespace.Equals("DynamiceCallWCF.Model"))
            //    obj = Utility.Convert(obj, Type.GetType(obj.GetType().Namespace.Replace("DynamiceCallWCF.Model", "DynamiceCallWCF.Model.Silverlight")));
            if (OnCompletedCallService != null)
            {
                if (_uc != null)
                {
                    _uc.Dispatcher.BeginInvoke(
                    delegate()
                    {
                        if (userstate == null)
                            OnCompletedCallService(this, new WCFEvent(obj));
                        else
                            OnCompletedCallService(this, new WCFEvent(obj) { UserState = userstate });
                    }

                );
                }
                else
                {
                    OnCompletedCallService(this, new WCFEvent(obj) { UserState = userstate });
                }


            }
           
        }
    }
}
